/**
 * 
 */
package org.pump.client.main;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import org.pump.client.util.MyConstant;
import org.pump.client.util.PumpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author liudong03
 *
 */
public class MyProperty {
	private static final Logger mylog = LoggerFactory
			.getLogger(MyProperty.class);
	public Properties conf = new Properties();
	public String username;
	public String hostname;
	public int hostport;
	public String password;
	public int readBufSize;
	public int writeBufSize;
	public int maxPacketSize;
	public int binlogPosition;
	public String binlogFile;
	public int conTimeOut;
	public String charset;
	public int queuesize;
	public int serverid;
	public String confFile;
	
	
	public int init(String confFile)
	{
		try {
			this.confFile = confFile;
			this.conf.load(new FileReader(confFile));
		} catch (FileNotFoundException e) {
			mylog.error("the conf path=[" + confFile +"] is not exists",e);
			return MyConstant.CONFILE_NOT_EXISTS;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			mylog.error("Io Exception",e);
			return MyConstant.CONFILE_IO_EXCEPTION;
		}
		
		if(!readProperty())
		{
			mylog.error("read property fail");
			return MyConstant.CONFIGURATION_ITEM_ERROR;
		}
		return MyConstant.SUCC;
		
	}
	public boolean readProperty()
	{
		this.hostname = PumpUtil.readPropertyString(this.conf,
				"mysql.hostname");
		this.hostport = PumpUtil.readPropertyInt(this.conf, "mysql.port");
		this.conTimeOut = PumpUtil.readPropertyInt(this.conf,
				"mysql.connect_time_out");
		if(this.hostname == null)
		{
			mylog.error("configuration [mysql.hostname] is not right");
			return false;
		}
		if(this.hostport <= 0)
		{
			mylog.error("configuration [mysql.port] is not right");
			return false;
		}
		if(this.conTimeOut <= 0)
		{
			mylog.error("configuration [mysql.connect_time_out] is not right");
			return false;
		}
		this.readBufSize = PumpUtil.readPropertyInt(this.conf,
				"client.read_buffer_size");
		this.writeBufSize = PumpUtil.readPropertyInt(this.conf,
				"client.write_buffer_size");
		if(this.readBufSize <= 0)
		{
			mylog.error("configuration [client.read_buffer_size] is not right");
			return false;
		}
		if(this.writeBufSize <= 0)
		{
			mylog.error("configuration [client.write_buffer_size] is not right");
			return false;
		}
		this.username = PumpUtil.readPropertyString(conf, "mysql.username");
		this.password = PumpUtil.readPropertyString(conf, "mysql.password");
		this.queuesize = PumpUtil.readPropertyInt(conf, "client.event_queue_size");
		if(this.username == null)
		{
			mylog.error("configuration [username] is null");
			return false;
		}
		if(this.password == null)
		{
			mylog.error("configuration [password] is null");
			return false;
		}
		if(this.queuesize <= 0)
		{
			mylog.error("configuration [client.event_queue_size] is null");
			return false;
		}
		
		this.binlogFile = PumpUtil.readPropertyString(conf, "mysql.binlog_file");
		this.binlogPosition = PumpUtil.readPropertyInt(conf, "mysql.binlog_position");
		if(this.binlogFile == null)
		{
			mylog.error("configuration [mysql.binlog_file] is null");
			return false;
		}
		if(this.binlogPosition < 0)
		{
			mylog.error("configuration [mysql.binlog_position] is null");
			return false;
		}
		
		this.serverid = PumpUtil.readPropertyInt(conf, "client.serverid");
		if(this.serverid < 0)
		{
			mylog.error("configuration [client.serverid] is not right");
			return false;
		}
		
		this.maxPacketSize = PumpUtil.readPropertyInt(conf, "mysql.max_packet_size");
		if(this.maxPacketSize <= 0)
		{
			mylog.error("configuration [mysql.max_packet_size] is not right");
			return false;
		}
		
		this.charset = PumpUtil.readPropertyString(conf, "mysql.charset");
		if(this.charset == null)
		{
			mylog.error("configuration [mysql.charset] is not right");
			return false;
		}
		return true;
		
	}
	
	public boolean save()
	{
		FileOutputStream output = null;
		try {
			output = new FileOutputStream(this.confFile,false);
			this.conf.store(output, "my configureation");
			output.flush();
			output.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			mylog.error("save exception",e);
			return false;
		}
		return true;
	}
}
